/*
 * Copyright 2009 Max Kugland
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.splink.deepsplink.page.params {
	/**
	 * Util class which provides methods to 
	 * 1) generate a full page id including page parameters
	 * 2) extract the page id from the full page id
	 * 3) extract the page parameters from the full page id
	 * 
	 * A full page id looks like this:
	 * http://www.test.com/#/projects?project=1&part=2/
	 * the extracted id would be projects
	 * and the extracted params project=1 and part=2
	 * 
	 * @see org.splink.deepsplink.page.params.PageParameter
	 * 
	 * @author Max Kugland
	 */
	final public class PageParameterUtil {
		private static const PARAMS_DELIM : String = "?";
		private static const KEY_VALUE_PAIR_DELIM : String = "&";
		private static const KEY_VALUE_DELIM : String = "=";

		private static function getParamsArray(pageParams : Array = null) : Array {
			if(pageParams) {
				return pageParams;
			} else {
				return new Array();
			}
		}

		/**
		 * Constructs a full id from the given page id and page parameters.
		 * 
		 * @param id the pageId
		 * @param params an array of <code>PageParameter</code> instances
		 */
		public static function constructId(id : String, params : Array = null) : String {
			params = getParamsArray(params);
			if(id.indexOf(PARAMS_DELIM) == -1 && params.length > 0) {
				id += PARAMS_DELIM;

				for each (var p : PageParameter in params) {
					if(p.key != null) {
						id += p.key + KEY_VALUE_DELIM + p.value + KEY_VALUE_PAIR_DELIM;
					}
				}

				if(id.charAt(id.length - 1) == KEY_VALUE_PAIR_DELIM) {
					id = id.substr(0, id.length - 1);
				}
			}
			
			// if there are no params, remove the <code>PARAMS_DELIM</code>
			if(id.substr(id.length - 1, id.length) == PARAMS_DELIM) {
				id = id.substr(0, id.length - 1);
			}

			return id;
		}

		/**
		 * Extracts an array of <code>PageParameter</code> instances from
		 * the given full page id (inclusive page parameters)
		 * 
		 * @param id the full page id (inclusive page parameters)
		 * @return an array of <code>PageParameter</code> instances
		 */
		public static function extractParams(id : String) : Array {
			var pageParams : Array = new Array;
			if(id.indexOf(PARAMS_DELIM) != -1) {
				var ar : Array = id.split(PARAMS_DELIM);
				id = ar[0];
				var params : String = ar[1];
				if(params.length == 0)
					return new Array;

				var a : Array;
				var pairs : Array;
				if(params.indexOf(KEY_VALUE_PAIR_DELIM) != -1) {
					pairs = params.split(KEY_VALUE_PAIR_DELIM);
					for each (var pair : String in pairs) {
						a = pair.split(KEY_VALUE_DELIM);
						if(a[0] != null) {
							pageParams.push(new PageParameter(a[0], a[1]));
						}
					}
				} else {
					a = params.split(KEY_VALUE_DELIM);
					if(a[0] != null) {
						pageParams.push(new PageParameter(a[0], a[1]));
					}
				}
			}

			return pageParams;
		}

		/**
		 * Extracts the id from the given full page id.
		 * 
		 * @param id the full page id (inclusive page parameters)
		 */
		public static function extractId(id : String) : String {
			if(id.indexOf(PARAMS_DELIM) != -1) {
				return id.split(PARAMS_DELIM)[0];
			}
			return id;
		}
	}
}
